//https://leetcode.cn/problems/flood-fill/
class Solution
{
    typedef pair<int, int> PII;
    int dx[4] = {0, 0, -1, 1};
    int dy[4] = {-1, 1, 0, 0};

public:
    vector<vector<int>> floodFill(vector<vector<int>> &image, int sr, int sc, int color)
    {
        int prev = image[sr][sc];
        if (prev == color)
            return image;
        int n = image.size(), m = image[0].size(); // 行与列
        // 宽搜，借助队列
        queue<PII> q;
        q.push({sr, sc});
        while (q.size())
        {
            auto [a, b] = q.front();
            q.pop();
            if (image[a][b] == prev)
                image[a][b] = color;
            // 开始搜索入队列
            for (int i = 0; i < 4; i++)
            {
                int x = a + dx[i], y = b + dy[i];
                if (x >= 0 && x < n && y >= 0 && y < m && image[x][y] == prev)
                    q.push({x, y});
            }
        }
        return image;
    }
};